package org.gwt.grin.client.features;

import org.gwt.grin.client.Feature;
import org.gwt.grin.client.util.Graphics;
import org.gwt.grin.client.util.Rectangle;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;

public class FixedImage extends Feature {

	protected Rectangle placement;
	protected ImageResource imageResource;
	protected InterpolatedModel scalingModel = null;
	protected Rectangle scaleBounds = null;

	private boolean isActivated;

	private Image image;

	public FixedImage(String name, int x,int y, ImageResource imageResource) {
		super(name);
		this.placement = new Rectangle(x, y, 0, 0);
		this.imageResource = imageResource;
	}

	@Override
	protected void initialize() {
		// do nothing
	}

	@Override
	public int getX() {
		return placement.x;
	}

	@Override
	public int getY() {
		return placement.y;
	}

	@Override
	protected void destroy() {
		if (image == null) {
			return;
		}
		if (image.isAttached()) {
			image.removeFromParent();
		}
		image = null;
	}

	@Override
	protected void setActivateMode(boolean mode) {
		isActivated = mode;
		if (mode) {
			markAsChanged();
		} else {
			image.removeFromParent();
		}
	}

	@Override
	protected int setSetupMode(boolean mode) {
		if (mode && image == null) {
			image = new Image(imageResource);
		}
		return 0;
	}

	@Override
	public boolean needsMoreSetup() {
		return false;
	}
	
	@Override
	public void nextFrame() {
		// do nothing
	}

	@Override
	public void paintFrame(Graphics gc) {
		if(!isActivated || !isChanged()){
			return;
		}
		gc.setBounds(image, placement.x, placement.y, 0, 0);
	}

}
